package com.service.Impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.utils.BaseService;
import com.utils.DataTables;
import com.utils.RequestStatus;
import com.dao.ScrapCollectorMapper;
import com.entity.Area;
import com.entity.HousingEstate;
import com.entity.ScrapCollector;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.ScrapCollectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ScrapCollectorServiceImpl extends BaseService implements ScrapCollectorService{
    @Autowired
    private ScrapCollectorMapper scrapCollectorMapper;


    @Override
    public List<ScrapCollector> selectScrapCollector(){
        return scrapCollectorMapper.selectScrapCollector();
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {

        PageHelper.startPage((dataTables.getStart() / dataTables.getLength()) + 1, dataTables.getLength()); // 核心分页代码

//        PageHelper.orderBy("sort asc");

        if(!StringUtils.isEmpty(dataTables.getColumn())){
            String column = propertyToColumn(ScrapCollectorMapper.class, dataTables.getColumn());
            if(!StringUtils.isEmpty(column)){
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }

        PageInfo<ScrapCollector> pageInfo = new PageInfo<ScrapCollector>(scrapCollectorMapper.getPageList(dataTables.getSearch(), dataTables.getSubSQL()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<ScrapCollector>());

        return dataTables;
    }


    @Override
    public Map<String, Object> delScrapCollector(List<Integer> idlist) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (scrapCollectorMapper.deleteByPrimaryKeys(idlist) > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    @Override
    public Map<String, Object> addScrapCollector(ScrapCollector scrapCollector) {
        Map<String, Object> map = new HashMap<String, Object>();
        Integer id = scrapCollectorMapper.insert(scrapCollector);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    @Override
    public Map<String, Object> updateScrapCollector(ScrapCollector scrapCollector) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (scrapCollectorMapper.updateByPrimaryKey(scrapCollector) > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    @Override
    public ScrapCollector getScrapCollectorById(Integer id) {
        return scrapCollectorMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Area> selectAllArea() {
        return scrapCollectorMapper.selectAllArea();
    }

    @Override
    public List<HousingEstate> getHousingEstateByCityCode(String cityCode) {
        return scrapCollectorMapper.getHousingEstateByCityCode(cityCode);
    }
}